﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class ExchangeRate: IExchangeRate
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public ExchangeRate(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.ExchangeRate GetExchangeRate(System.Int32 exchangeRateID)
		{
			BEGDC.PreContract.DAL.ExchangeRate exchangeRate = BEGDC.PreContract.DAL.ExchangeRate.TryFind(exchangeRateID);

            return exchangeRate;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetExchangeRate_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM ExchangeRate";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteExchangeRate(System.Int32 exchangeRateID)
		{
			try
            {
				BEGDC.PreContract.DAL.ExchangeRate exchangeRate = BEGDC.PreContract.DAL.ExchangeRate.TryFind(exchangeRateID);
                exchangeRate.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("ExchangeRate", string.Format("Error occured when deleting the ExchangeRate, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddExchangeRate(Nullables.NullableDecimal rate, Nullables.NullableDateTime updateDate)	
		{
			try
            {
				BEGDC.PreContract.DAL.ExchangeRate exchangeRate = new BEGDC.PreContract.DAL.ExchangeRate();
				
				exchangeRate.Rate = rate;
				exchangeRate.UpdateDate = updateDate;
				exchangeRate.CreateAndFlush();
				
				return exchangeRate.ExchangeRateID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("ExchangeRate", string.Format("Error occured when adding a new ExchangeRate, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateExchangeRate(System.Int32 exchangeRateID, Nullables.NullableDecimal rate, Nullables.NullableDateTime updateDate)
		{
			try
            {
                BEGDC.PreContract.DAL.ExchangeRate exchangeRate = GetExchangeRate(exchangeRateID);

				exchangeRate.Rate = rate;
				exchangeRate.UpdateDate = updateDate;
				exchangeRate.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("ExchangeRate", string.Format("Error occured when updating the ExchangeRate, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



